<template>
  <div class="my-container">

    <div v-if="user" class="my-header">
      <div class="have-login">
        <div class="top">
          <div class="left">
            <van-image src="https://img01.yzcdn.cn/vant/cat.jpeg" round fit="cover" />
            <span>头条君</span>
          </div>
          <div class="right">
            <van-button type="default" round>编辑资料</van-button>
          </div>
        </div>
        <div class="fot">
          <div class="fot-item">
            <span>11</span>
            <span>头条</span>
          </div>
          <div class="fot-item">
            <span>11</span>
            <span>关注</span>
          </div>
          <div class="fot-item">
            <span>11</span>
            <span>粉丝</span>
          </div>
          <div class="fot-item">
            <span>11</span>
            <span>获赞</span>
          </div>
        </div>
      </div>
    </div>
    <div v-else class="my-header">
      <div class="not-login" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" alt>
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <van-grid clickable class="my-grid" :column-num="2">
      <van-grid-item>
        <i slot="icon" class="iconfont icon-shoucang" />
        <span slot="text" class="text">收藏</span>

      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="iconfont icon-lishi" />
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>

    <van-cell-group>
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
      <van-cell v-if="user" clickable title="退出登录" class="logout-btn" @click="logout" />
    </van-cell-group>
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'MyIndex',
  computed: {
    ...mapState('user', ['user'])
  },
  methods: {
    ...mapMutations('user', ['setUser']),
    logout() {
      this.$dialog.confirm({
        title: '退出',
        message: '确定要退出登录吗'
      })
        .then(() => {
          // on confirm
          this.setUser(null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async getUserInfo() {
      try {
        const res = await getUserInfo()
        console.log(res)
      } catch (err) {
        this.$toast.fail('获取用户信息失败')
      }
    }
  },
  created() {
    this.getUserInfo()
  }
}
</script>
<style lang="less" scoped>
.my-header {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 361px;
  background: url("~@/assets/banner.png");
  background-size: cover;
  .not-login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
      margin-bottom: 20px;
    }
    .text {
      font-size: 28px;
      color: #fff;
    }
  }
  .have-login {
    width: 100%;
    .top {
      display: flex;
      justify-content: space-around;
      align-items: center;
      padding: 100px 0 10px 0;
      box-sizing: border-box;
      .left {
        display: flex;
        justify-content: center;
        align-items: center;
        .van-image {
          width: 100px;
          height: 100px;
          margin-right: 10px;
        }
        span {
          color: #fff;
          font-size: 48px;
        }
      }
      .right {
        .van-button {
          height: 60px;
        }
      }
    }
    .fot {
      display: flex;
      margin-top: 60px;
      .fot-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        span:nth-child(1) {
          font-size: 28px;
        }
        span:nth-child(2) {
          font-size: 30px;
        }
      }
    }
  }
}
.my-grid{
  .iconfont{
    font-size: 45px;
  }
  .icon-shoucang{
    color:red;
  }
  .icon-lishi{
    color:yellow;
  }
  .text{
    font-size: 20px;
  }
}
.logout-btn{
  text-align: center;
  color: red;
}
</style>
